<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of categories
 *
 * @author QUYET
 */
class admin extends Admin_Controller {

    //put your code here
    public function __construct() {
        parent::__construct();
        $this->load->model('admin_m');
        $this->load->library('category');
        $this->load->library('form_validation');
        $this->load->library('pagination');
        $this->load->helper('category_helper');
        $this->category->build_option_data(1, 'Categories', 0);
    }

    function index() {
        $this->listing();
    }

    function listing() {
        $this->session->set_userdata('current_url', current_url());
        $this->_data['module_title'] = __('cat_list');
        $off_set = $this->uri->segment(4) != '' ? $this->uri->segment(4) : 0;
        $per_page = 10;
        $config['full_tag_open'] = "<div class='page'>";
        $config['full_tag_close'] = '</div>';
        $config['cur_tag_open'] = '<span> ';
        $config['cur_tag_close'] = ' </span>';
        $config['next_link'] = 'Next';
        $config['prev_link'] = 'Previous';
        $config['first_link'] = 'First';
        $config['last_link'] = 'Last';
        $config['base_url'] = base_url() . 'admin/categories/listing/';
        $config['per_page'] = $per_page;
        $config['uri_segment'] = 4;
        $config['total_rows'] = $this->db->count_all('ots_ticket_cat');
        if ($off_set > 0 && $off_set == $config['total_rows']) {
            $off_set = $off_set - $per_page;
        }
        $this->_data['categories'] = $this->admin_m->get_all_categories($per_page,$off_set);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['off_set'] = $off_set;
        $this->display_admin('admin/listing');
    }
    
    function delete($cat_id,$off_set=0){
        if($cat_id==1){
            return;
        }
        $tickets = $this->admin_m->get_all_tickets($cat_id);
        foreach ($tickets as $ticket) {
            $this->db->update('ots_ticket',array('cat_id'=>1),array('id'=>$ticket['id']));
        }
        $this->db->delete('ots_ticket_cat',array('id'=>$cat_id));
        redirect(base_url() . 'admin/categories/listing/'.$off_set);
    }

    function add() {
        $this->session->set_userdata('current_url', current_url());
        $this->_data['category_options'] = $this->category->option_store;
        if (isset($_POST['btnSubmit'])) {
            //Array ( [cat_name] => ss [cat_id] => 2 [btnSubmit] => Add Category ) 
            $this->form_validation->set_rules('cat_name',__('categories'), 'required');
            $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
            if (!$this->form_validation->run()) {
                $this->_data['errors'] = validation_errors();
                $this->display_admin('admin/add');
            }
            $lang = 'fr';
            if (isset($_POST['lang'])) {
                $lang = $_POST['lang'];
            }
            $data = array(
                'category' => $_POST['cat_name'],
                'parent_id' => $_POST['cat_id'],
                'depth' => generate_category_depth($_POST['cat_id'])
            );
            $this->admin_m->add_category($data, $lang);
            redirect(base_url() . 'admin/categories/listing');
        } else {
            $this->display_admin('admin/add');
        }
    }

    
    function edit($id_cat) {
        $this->session->set_userdata('current_url', current_url());
        $this->_data['category_options'] = $this->category->option_store;
        $this->_data['category'] = $this->admin_m->get_cat_details($id_cat);
        if(empty($this->_data['category'])) redirect('admin/categories');
        if (isset($_POST['btnSubmit'])) {
            //Array ( [cat_name] => ss [cat_id] => 2 [btnSubmit] => Add Category ) 
            $this->form_validation->set_rules('cat_name', 'Category', 'required');
            $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
            if (!$this->form_validation->run()) {
                $this->_data['errors'] = validation_errors();
                $this->display_admin('admin/edit');
            }
            $lang = 'fr';
            if (isset($_POST['lang'])) {
                $lang = $_POST['lang'];
            }
            $data = array('category' => $_POST['cat_name']);
            if($id_cat>2){
                $data['parent_id']=$_POST['cat_id'];
                $data['depth']=generate_category_depth($_POST['cat_id']);
            }
            $this->admin_m->update_category($data,$id_cat);
            redirect(base_url() . 'admin/categories/listing');
        } 
        $this->display_admin('admin/edit');
    }
}

?>
